// void, Obj This

Building this; 
Query qO;
Unit u, u1, u2;
ObjList olVolunteer, ol, ol_outpost, olMilitia;
Query qRange, qEnemies;
int player, r, i, k, max_unit, j, countv, countm;
str class_unit, class_unitp;
point p, pos;
rect rc;

Sleep(10000+rand(GetConst("RandTimeStart"))+GetConst("AddTimeStart"));

this = This.AsBuilding();
//pr("village dziala");
qO = ClassPlayerObjs("Outpost", .player);

while(.IsValid){
	Sleep(3000);
	if(IsAIPlayer(.player)){
		while(.IsValid) {
			Sleep(60000);
			if(!IsAIPlayer(.player)) break;
		}
			
	}
	
	if(player != .player){
		r = EnvReadInt(.player, "RACE");

		if (r==Carthage){
			class_unit = "CMaceman";
			class_unitp = "CLibyanFootman";
		} else if (r==Iberia){
			class_unit = "ISlinger";
			class_unitp = "IMilitiaman";
		} else if (r==Britain){
			class_unit = "BShieldBearer";
			class_unitp = "BSwordsman";
		} else if (r==Germany){
			class_unit = "TAxeman";
			class_unitp = "TSwordsman";
		} else if (r==Egypt){
			class_unit = "EAnubisWarrior";
			class_unitp = "ESwordsman";
		} else if (r==Gaul){
			class_unit = "GAxeman";
			class_unitp = "GSwordsman";
		} else if(r == ImperialRome || r == RepublicanRome){
			class_unit = "RGladiator";
			class_unitp = "RHastatus";
		} else {
			class_unit = "CMaceman";
			class_unitp = "IMilitiaman";
		}

		player = .player;
	}
	if(EnvReadString(.settlement, "militia") == "yes"){
		qRange = ObjsInRange(this, "Unit", 700);
		qEnemies = Intersect(qRange, Union(EnemyObjs(.player, "Military"),EnemyObjs(.player, "BaseMage")));
		qEnemies = Subtract(qEnemies,EnemyObjs(.player, "Sentry"));
		//pr("c0");
		if(!qEnemies.IsEmpty()){
			//pr("c1");
			olMilitia.ClearDead();
			if(olMilitia.count < 10){
				//pr("c2");
				if(.settlement.population > (.settlement.max_population/2)){
					
					countm = 10 - olMilitia.count;
					//pr(countm);
					//pr("c3");
					for(i=0; i<countm; i+=1){
								
						if(.settlement.Units.count < 40){
							Sleep(rand(100)+100);
							u1 = Place(class_unitp, .pos, player);
							if(u1.IsValid){
								//if (u.AsUnit().GetCommanded())
								u1.SetFeeding(false);
								u1.SetNoselectFlag(true);
								u1.SetLevel(10);
								u1.AddItem("Veteran Offence");
								.settlement.ForceAddUnit(u1);
								olMilitia.Add(u1); 
								.settlement.SetPopulation(.settlement.population - 1);  
							}
						}
						Sleep(100);
					}
					//pr(.settlement.Units.count);
					//for(i=0; i<.settlement.Units.count; i+=1){
					for(k=0; k<olMilitia.count; k+=1){
									
						//u2 = .settlement.Units[i].AsUnit;
						u2 = olMilitia[k].AsUnit;
						if(u2.IsValid){
							//u1.SetCommand("attach", Leader);
							rc = GetMapRect();
							pos.Set(0, .sight / 2);
							pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); u2.SetCommand("advance", p);
							pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); u2.AddCommand(false, "patrol", p);
							pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); u2.AddCommand(false, "patrol", p);
							pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); u2.AddCommand(false, "patrol", p);
							pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); u2.AddCommand(false, "patrol", p);
						}
						Sleep(rand(50)+50);
					}
				}
			}
		} else {
			olMilitia.ClearDead();
			for(k=0; k<olMilitia.count; k+=1){
			
				u2 = olMilitia[k].AsUnit;
				if(u2.IsValid){
					if(u2.command != "enterp")
						u2.SetCommand("enterp", this);
				}
				Sleep(rand(50)+50);
			}
			//if(olMilitia.count >0){
				//olMilitia.SetCommand("enterp", this);
			//}	
		}
	}
	//Sleep(400000);
	j+=1;
	if(j >= 130){
		j = 0;
		if(EnvReadInt(.player, "LeaderOfNation") == 1){
			
			if(!.IsBroken() && !.IsVeryBroken()){
				
				olVolunteer.ClearDead();
				countv = olVolunteer.count;
				for(i=0; i<countv; i+=1){
					u = olVolunteer[i];
					if(u.IsValid)
						if (u.AsUnit().GetCommanded()){
							u.SetFeeding(true);
							olVolunteer.Remove(u);
						}
				}        
					
				ol = GetPlayerUnits(.player, class_unit);
				
				if(ol.count < 200){
				   
					//item = Leader.GetItem(1);
					ol_outpost = qO.GetObjList();
					//max_unit = 0;
					//max_unit = item.use_count/10;    
					max_unit = ol_outpost.count;
					for(i=0; i<max_unit; i+=1){
						
						if(.settlement.Units.count < 40){
							//pr("Place 2");
							Sleep(rand(100)+100);
							u1 = Place(class_unit, .pos, player);
							if(u1.IsValid){
								//if (u.AsUnit().GetCommanded())
								u1.SetFeeding(false);
								u1.SetLevel(max_unit);
								.settlement.ForceAddUnit(u1);
								olVolunteer.Add(u1); 
								//pr("unit add");   
							}
						}
						Sleep(100);
					}
		 
					if(EnvReadString(.settlement, "tribute_people") != "on"){

						for(i=0; i<.settlement.Units.count; i+=1) {
							
							u2 = .settlement.Units[i].AsUnit;
							if(u2.IsValid){
								//u1.SetCommand("attach", Leader);
								rc = GetMapRect();
								pos.Set(0, .sight / 2);
								pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); u2.SetCommand("advance", p);
								pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); u2.AddCommand(false, "patrol", p);
								pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); u2.AddCommand(false, "patrol", p);
								pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); u2.AddCommand(false, "patrol", p);
								pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); u2.AddCommand(false, "patrol", p);
							}
							Sleep(rand(50)+50);
						}
					}
				}
			}
		}
	}
}

